$(document).ready(function(){
    var _self = $('#SELF').val();
    //var product_id = $('#product_id').val();
    $("#tabs").tabs();
    $('.category_tree').ajaxTree({
        url : $('#APP_URL').val()+'/index.php/request/category/module/product',
        data : function(node){
            return {
                id : node.attr("id").substr(0, 5) == 'node_' ? node.attr("id").substr(5) : 0
            };
        },
        check : node_check
    });
    get_gallery_list($('#product_id').val());


    $('#is_promote').click(function(){
        $('#promote_price, #promote_start_time, #promote_end_time').attr('disabled',!$(this).attr('checked'));
    });
    $('#is_auto_generate').click(function(){
        if($(this).attr('checked')){
            $('#thumb_img_upload_tr').hide();
            $('#thumb_img').val('');
        }else{
            $('#thumb_img_upload_tr').show();
        }
    });

    //日期控件，开始日期-结束日期
    var dates = $( "#promote_start_time, #promote_end_time" ).datepicker({
        defaultDate: "+1w",
        dateFormat: "yy-mm-dd",
        changeMonth: true,
        onSelect: function( selectedDate ) {
            var option = this.id == "promote_start_time" ? "minDate" : "maxDate",
            instance = $( this ).data( "datepicker" ),
            date = $.datepicker.parseDate(
                instance.settings.dateFormat ||
                $.datepicker._defaults.dateFormat,
                selectedDate, instance.settings );
            dates.not( this ).datepicker( "option", option, date );
        }
    });

    //文件上传
    $('#original_img_upload_form').fileUploadUI({
        uploadTable: $('#original_img_upload'),
        buildUploadRow: function (files, index, handler) {
            return $('<tr><td>' + files[index].name + '<\/td>' +
                    '<td class="file_upload_progress"><div><\/div><\/td>' +
                    '<td class="file_upload_cancel">' +
                    '<button class="ui-state-default ui-corner-all" title="Cancel">' +
                    '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
                    '<\/button><\/td><\/tr>');
        },
        buildDownloadRow: function (file, handler) {
            $('#product_img').val(file.url);
            if(file.thumbnail_url) $('#product_thumb').val(file.thumbnail_url);
            $('#original_img_upload').html(file.name);
            //return $('<tr><td>' + file.name + '<\/td><\/tr>');
        }
    });
    $('#thumb_img_upload_form').fileUploadUI({
        uploadTable: $('#thumb_img_upload'),
        buildUploadRow: function (files, index, handler) {
            return $('<tr><td>' + files[index].name + '<\/td>' +
                    '<td class="file_upload_progress"><div><\/div><\/td>' +
                    '<td class="file_upload_cancel">' +
                    '<button class="ui-state-default ui-corner-all" title="Cancel">' +
                    '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
                    '<\/button><\/td><\/tr>');
        },
        buildDownloadRow: function (file, handler) {
            $('#product_thumb').val(file.url);
            $('#thumb_img_upload').html(file.name);
            //return $('<tr><td>' + file.name + '<\/td><\/tr>');
        }
    });
    $('#gallery_img_upload_form').fileUploadUI({
        uploadTable: $('#gallery_img_upload'),
        buildUploadRow: function (files, index, handler) {
            return $('<tr><td>' + files[index].name + '<\/td>' +
                    '<td class="file_upload_progress"><div><\/div><\/td>' +
                    '<td class="file_upload_cancel">' +
                    '<button class="ui-state-default ui-corner-all" title="Cancel">' +
                    '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
                    '<\/button><\/td><\/tr>');
        },
        buildDownloadRow: function (file, handler) {
            $('#gallery_img').val(file.url);
            $('#gallery_thumb').val(file.thumbnail_url);
            $('#gallery_img_upload').html(file.name);
            //$('#gallery_img_upload').html(file.name);
            //return $('<tr><td>' + file.name + '<\/td><\/tr>');
        }
    });

    $('#save_basic').click(function(){
        var data = $('#table_basic input').serialize();
        $.post(_self+'/action/save_basic'+($('#product_id').val() ? '/product_id/'+$('#product_id').val() : ''), data, function(data){
            if(data.status == '1'){
                $('#product_id').val(data.data.product_id);
                $('#product_sn').val(data.data.product_sn);
            }
            alert(data.info);
        }, 'json');
    });

    $('#save_desc').click(function(){
        if($('#product_id').val() == '0'){
            alert('请先填写基本信息');
            $('#basic_link').click();
        }else{
            var oEditor = CKEDITOR.instances.detail_desc;
            $.post(_self,{action:'save_desc',product_id:$('#product_id').val(),content:oEditor.getData()},function(data){
                alert('保存'+(data.status == '1' ? '成功' : '失败'));
            },'json');
        }
    });

    $('#save_info').click(function(){
        if($('#product_id').val() == '0'){
            alert('请先填写基本信息');
            $('#basic_link').click();
        }else{
            var data = $('#table_info input').serialize();
            $.post(_self+'/action/save_info/product_id/'+$('#product_id').val(), data, function(data){
                alert('保存'+(data.status == '1' ? '成功' : '失败' ));
            }, 'json');
        }
    });
    $('#save_gallery').click(function(){
        if($('#product_id').val() == '0'){
            alert('请先填写基本信息');
            $('#basic_link').click();
        }else if($('#gallery_img').val() == '' || $('#gallery_thumb').val() == ''){
            alert('请先选择文件');
        }else{
            $.post(_self,{action:'save_gallery',product_id:$('#product_id').val(),img_url:$('#gallery_img').val(),thumb_url:$('#gallery_thumb').val(),img_desc:$('#gallery_img_desc').val()},function(data){
                alert('提交'+(data.status == '1' ? '成功' : '失败'));
                $('#gallery_img_desc').val('');
                $('#gallery_img_upload').html('');
                get_gallery_list($('#product_id').val());
            },'json');
        }
    });
});

function node_check(node){
    var tree_type = node.parents('div').attr('id');
    var id = node.attr("id").substr(5);
    if(id > 1){
        var name = node.find("a:first").text();
        var order_by = node.attr("order_by");
        var action = node.find(':checkbox:first').attr('checked');

        change_category(id,name,order_by,action,tree_type);
    }
}

function change_category(id,name,order_by,action,tree_type){
    switch(tree_type){
        case 'product':
            break;
        case 'accessory':
            break;
        case 'article':
            break;
        case 'product_basic_tree':
            if (action){
                $('#cat_name').val(name);
                $('#cat_id').val(id);
            }else{
                if ($('#cat_name').val() == name){
                    $('#cat_name').val('');
                    $('#cat_id').val('');
                }
            }
            break;
    }
}

function get_gallery_list(product_id){
    if(product_id!= ''){
        $.post($('#SELF').val(),{action:'get_gallery_list',product_id:product_id},function(data){
            $('#get_gallery_list').html(data);
        });
    }
}

function del_gallery(type,img_id){
    $.post($('#SELF').val(),{action:'del_gallery',type:type,gallery_img_id:img_id},function(data){
        get_gallery_list($('#product_id').val());
    });
}

function re_gallery(img_id){
    $.post($('#SELF').val(),{action:'re_gallery',gallery_img_id:img_id},function(data){
        get_gallery_list($('#product_id').val());
    });
}

function get_assoc_list(type){
    $.post($('#SELF').val(),{action:'get_assoc_list',type:type},function(data){
        get_gallery_list($('#product_id').val());
    });
}